package com.power.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.power.domain.MemberAddr;
import com.power.model.Result;
import com.power.service.MemberAddrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 会员收货地址管理
 * @Author dingchunlin
 * @Date 2024/07/20 17:33
 * @Version 1.0.0
 */
@Api(tags = "会员收货地址接口管理")
@RequestMapping("/p/address")
@RestController
public class MemberAddrController {

    @Autowired
    private MemberAddrService memberAddrService;

    /**
     * 查询会员所有收货地址
     * @return 会员所有收货地址列表
     */
    @ApiOperation(value = "查询会员所有收货地址")
    @GetMapping("/list")
    public Result<List<MemberAddr>> loadAllMemberAddr() {
        List<MemberAddr> memberAddrList = memberAddrService.queryAllMemberAddr();
        return Result.success(memberAddrList);
    }


    /**
     * 修改用户的收货地址
     * @param memberAddr 收货地址
     * @return 成功或失败
     */
    @ApiOperation(value = "修改用户的收货地址")
    @PutMapping
    public Result<String> modifyMemberAddr(@RequestBody MemberAddr memberAddr) {
        Boolean modify = memberAddrService.modifyMemberAddr(memberAddr);
        return Result.handle(modify);
    }

    /**
     * 用户添加收货地址
     * @param memberAddr 收货地址
     * @return 成功或失败
     */
    @ApiOperation(value = "用户添加收货地址")
    @PostMapping
    public Result<String> addMemberAddr(@RequestBody MemberAddr memberAddr) {
        Boolean save = memberAddrService.saveMemberAddr(memberAddr);
        return Result.handle(save);
    }

    /**
     * 修改用户默认收货地址
     * @param addrId
     * @return
     */
    @ApiOperation(value = "修改用户默认收货地址")
    @PutMapping("/defaultAddr/{addrId}")
    public Result<String> modifyDefaultAddr(@PathVariable Long addrId) {
        Boolean modify = memberAddrService.modifyDefaultAddr(addrId);
        return Result.handle(modify);
    }

    /**
     * 根据标识查询收货地址
     * @param addrId
     * @return
     */
    @ApiOperation(value = "根据标识查询收货地址")
    @GetMapping("/addrInfo/{addrId}")
    public Result<MemberAddr> loadMemberAddrById(@PathVariable Long addrId) {
        MemberAddr memberAddr = memberAddrService.getById(addrId);
        return Result.success(memberAddr);
    }

    /**
     * 删除收货地址
     * @param addrId 收货地址id
     * @return 成功或失败
     */
    @ApiOperation(value = "删除收货地址")
    @DeleteMapping("/deleteAddr/{addrId}")
    public Result<String> removeMemberAddrById(@PathVariable Long addrId) {
        Boolean remove = memberAddrService.removeById(addrId);
        return Result.handle(remove);
    }










    /**
     * 根据收货地址id查询收货地址列表
     * @param addrId 收货地址id
     * @return 收货地址
     */
    @GetMapping("/getMemberAddrById")
    public Result<MemberAddr> getMemberAddrById(@RequestParam Long addrId){
        MemberAddr memberAddr = memberAddrService.getById(addrId);
        return Result.success(memberAddr);
    }

    /**
     * 根据openId查询默认收货地址
     * @param openId 用户openId
     * @return 默认收货地址
     */
    @GetMapping("/getMemberDefaultAddrByOpenId")
    public Result<MemberAddr> getMemberDefaultAddrByOpenId(@RequestParam String openId){
        MemberAddr memberAddr = memberAddrService.getOne(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
                .eq(MemberAddr::getCommonAddr, 1)
                .eq(MemberAddr::getStatus, 1));
        return Result.success(memberAddr);
    }
}
